package bd403.utils;

import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/* Utility class to setup the keyboard settings for the application.
 * Main author: Joe
 */

public class KeyboardSetup {

	private static KeyboardSetup instance = null;
	private static Context context;
	
	protected KeyboardSetup() {
		// Singleton pattern, exists to guard against instantiation.
	}
	
	/* Returns an instance of this KeyboardSetup, or a new instance if one does not yet
	 * exist.
	 */
	public static KeyboardSetup getInstance(Context c) {
		if (instance == null) {
			instance = new KeyboardSetup();
		}
		context = c;
		return instance;
	}
	
	/* Initialize the keyboard settings for the application.
	 */
	public void setupKeyboard(final EditText et) {
		et.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // If the event is a key-down event on the "enter" button
                if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                    (keyCode == KeyEvent.KEYCODE_ENTER)) {
                	// Perform action on hide keyboard
                    InputMethodManager mgr = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                    mgr.hideSoftInputFromWindow(et.getWindowToken(), 0);
                  return true;
                }
                return false;
            }
        });
	}
}
